package com.woniumall.web;

import com.woniumall.pojo.Massage;
import com.woniumall.pojo.User;
import com.woniumall.service.UserService;
import com.woniumall.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @auther linke
 * @create 2021-10-29 16:46
 */
@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    //获取服务对象
    private UserService userService = new UserServiceImpl();

    /**
     * 检查用户名是否已存在
     * @param request
     * @param response
     */
    public void checkUsername(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        Massage massage = userService.checkUsername(username);
        responseUtil(response,massage);
    }
    //用户头像上传处理
    public void uploadhead(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String saveFilename = (String) request.getSession().getAttribute("saveFilename");//获取用户上传后的文件名
        // String savafile = "upload"+"\\"+"userimage"+"\\"+saveFilename;
        System.out.println(saveFilename);
        User user = (User)request.getSession().getAttribute("user");
        int row = userService.updateImage(user.getId(), saveFilename);//根据用户名，和图片地址修改user头像
        if (row < 1){
            System.out.println("修改失败");
        }
        System.out.println(saveFilename);
        request.getRequestDispatcher("/personal/index").forward(request,response);
    }

    //注册
    public void register(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = new User();

        BeanUtils.populate(user,parameterMap);
        userService.register(user);

        response.sendRedirect(request.getContextPath()+"/jspPage/register_ok.jsp");
    }
    //激活用户
    public void active(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String code = request.getParameter("code");
        userService.active(code);

        response.sendRedirect(request.getContextPath()+"/jspPage/login.jsp");
    }
    //用户登录查询
    public void checkLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        Massage massage = userService.checkLogin(username);
        responseUtil(response,massage);
    }
    //用户登录
    public void login(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = userService.login(username, password);
        if(user == null ){
            //用户不存在，密码错误
            Massage msg = new Massage(false,"请检查用户名或密码");
            request.getRequestDispatcher("/jspPage/login.jsp").forward(request, response);
            responseUtil(response,msg);
        }else {
            if(user.getStatus().equals("1")){  //用户密码匹配成功且激活
                //用户匹配正确跳转首页面
                //1.向域中存储信息5
                HttpSession session = request.getSession();
                session.setAttribute("user", user);
                Massage msg = new Massage(true,"登录成功");
                response.sendRedirect(request.getContextPath()+"/index.jsp");
            }else{//未激活
                Massage msg = new Massage(false,"邮箱未激活,请先激活再登录");
                request.getRequestDispatcher("/jspPage/login.jsp").forward(request, response);
                responseUtil(response,msg);
            }
        }
    }
    public void quit(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getSession().removeAttribute("user");
        response.sendRedirect(request.getContextPath()+"/index.jsp");
    }
    //修改密码中校验原密码
    public void checkPassword(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String password = request.getParameter("password");//获取页面传输的密码
        Massage msg = new Massage();
        User user = (User)request.getSession().getAttribute("user");//获取user对象
        if(password.equals(user.getPassword())){//密码输入正确
            msg.setFlag(true);
            msg.setMassage("密码正确");
        }else{
            msg.setFlag(false);
            msg.setMassage("密码错误，请联系管理员！");
        }
        responseUtil(response,msg);//响应提示信息
    }
    //确认修改密码
    public void updatePassword(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String password = request.getParameter("password");//获取修改后的密码
        User user = (User)request.getSession().getAttribute("user");//获取user对象
        int row = userService.updatePassword(user.getId(),password);
        if(row>0){//修改成功
            request.getSession().removeAttribute("user");
            response.sendRedirect(request.getContextPath()+"/jspPage/login.jsp");
        }else {
            request.getRequestDispatcher("/personal/index").forward(request,response);
        }

    }
}
